package com.hcss.common.redis.base.impl;

import com.hcss.common.redis.base.RedisValueDao;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @描述: String类型RedisTemplate Dao层实现
 * @作者: xuqian
 * @创建日期: 2018-01-12 16:42
 * @公司 浙江鸿程计算机系统有限公司
 */
@Repository("redisValueDao")
public class RedisValueDaoImpl<T> extends BaseRedisDaoImpl<T> implements RedisValueDao<T> {

    private final ValueOperations<String, T> valueOperations;

    @Autowired
    public RedisValueDaoImpl(RedisTemplate redisTemplate) {
        super(redisTemplate);
        this.valueOperations = redisTemplate.opsForValue();
    }

    @Override
    public ValueOperations getValueOperations() {
        return valueOperations;
    }

    @Override
    public void insert(String key, T value) {
        valueOperations.set(key, value);
    }

    @Override
    public void insert(String key, T value, long timeout, TimeUnit unit) {
        valueOperations.set(key, value, timeout, unit);
    }

    @Override
    public void insert(String key, T value, long offset) {
        valueOperations.set(key, value, offset);
    }

    @Override
    public void batchInsert(Map<String, T> values) {
        valueOperations.multiSet(values);
    }

    @Override
    public T selectObject(String key) {
        return valueOperations.get(key);
    }

    @Override
    public List<T> selectByKeys(Collection<String> keys) {
        if (CollectionUtils.isEmpty(keys)) {
            return null;
        }
        return valueOperations.multiGet(keys);
    }

    @Override
    public String split(String key, long start, long end) {
        return valueOperations.get(key, start, end);
    }

	@Override
	public T selectByKey(String key) {
		return valueOperations.get(key);
	}

}
